from django.db import models
from account_sys.models import *
from utils.time_utils import *


# Create your models here.
class Message(models.Model):
    sender = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='sent_messages')
    receiver = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='received_messages')
    msg_type = models.CharField(max_length=100, null=False, default='text')
    content = models.CharField(max_length=200, null=False)
    send_time = models.DateTimeField(null=True)
    file = models.CharField(max_length=100, null=False, default='')
    set_read = models.BooleanField(default=False)
    def parse_dict(self):
        return {
            "type": "chat.message",
            'msg_id': self.id,
            'sender_id': self.sender.id,
            'receiver_id': self.receiver.id,
            'content': self.content,
            'send_time': datetime2str(self.send_time),
            'file': self.file,
            'msg_type': self.msg_type,
            'set_read': self.set_read
        }
